﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Collections;

namespace WindowsFormsApplication1
{

    public class CTerrainLibList
    {
        public ArrayList pElements 
        {
            get;set;
        }
        
        public CTerrainLibList()
        {
            pElements = new ArrayList();
        }

        public bool IsFound(String fName)
        {
            for (int i = 0; i < pElements.Count; i++ )
            {
                if (((CTerrainElement)pElements[i]).mFileName == fName)
                {
                    return true;
                }
            }
            return false;
        }


        public CTerrainElement FindElement(String fName)
        {
            for (int i = 0; i < pElements.Count; i++)
            {
                if (((CTerrainElement)pElements[i]).mFileName == fName)
                {
                    return ((CTerrainElement)pElements[i]);
                }
            }
            return null;
        }

        public void Read(String fileName)
        {
            using (XmlTextReader xmlread = new XmlTextReader(fileName))
            {
                xmlread.ReadStartElement("TerrainList");
                while (true)
                {
                    if (xmlread.IsStartElement("Element"))
                    {
                        try
                        {
                            String con = xmlread.ReadString();
                            

                            if (con == "")
                            {
                                System.Windows.Forms.MessageBox.Show("地表库包含空元素！");
                                break;
                            }
                            else
                            {
                                
                                CTerrainElement m = new CTerrainElement();
                                m.mFileName = con;

                                if ( IsFound( con ))
                                {
                                    System.Windows.Forms.MessageBox.Show("地表库包含重复元素！");
                                    continue;
                                }

                                if (m.Read(con))
                                {
                                    pElements.Add(m);
                                }
                            }

                            xmlread.ReadEndElement();
                        }
                        catch(System.Exception)
                        {

                        }
                        
                    }
                    else break;
                }
            }
        }
        public void Save(String fileName)
        {
            using (XmlTextWriter xmlwrite = new XmlTextWriter(fileName, Encoding.Default))
            {
                xmlwrite.Formatting = Formatting.Indented;
                xmlwrite.WriteStartDocument();
                xmlwrite.WriteStartElement("TerrainList");

                for (int i = 0; i < pElements.Count; i++)
                {
                    xmlwrite.WriteStartElement("Element");
                    xmlwrite.WriteString(((CTerrainElement)pElements[i]).mFileName);
                    xmlwrite.WriteEndElement();
                }

                xmlwrite.WriteEndElement();
            }
        }
    }
}
